var Geolocation = {
  rad: function(x) { return x * Math.PI / 180 },

  // Distance in kilometers between two points using the Haversine algo.
  haversine: function(p1, p2) {
    var R = 6371
    var dLat  = this.rad(p2.latitude - p1.latitude)
    var dLong = this.rad(p2.longitude - p1.longitude)

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(this.rad(p1.latitude)) * Math.cos(this.rad(p2.latitude)) * Math.sin(dLong/2) * Math.sin(dLong/2)
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
    var d = R * c * 1000

    return d
  },
  
  // convert latitude and longitude to xy coordinate
  direction: function(p1Att, p1Lon, p2Att, p2Lon){
	  var R = 6371;
	  
	  var diffLatitude = (p1Att-p2Att)*Math.PI/180.0;
	  var difLongitude = (p1Lon-p2Lon)*Math.PI/180.0;
	  //var diffLatitude = (p1.latitude-p2.latitude)*Math.PI/180.0; /* transfer to rad -90~90 */
	 // var difLongitude = (p1.longitude-p2.longitude)*Math.PI/180.0; /*-180 ~ 180*/
	  
	  var direction = new Object();
	  direction.diffNS = diffLatitude*R;
	  direction.diffEW = diffLatitude*R;
	  
	  var p = 2*Math.PI*R;
	  direction.diffNS /= p;
	  direction.diffEW /=p;
	  
	  return direction;
  },
}
